package com.js.nickcar.module.login;

import com.js.nickcar.R;
import com.js.nickcar.api.HttpHelpter;
import com.js.nickcar.api.UserHelper;
import com.js.nickcar.base.ApiCallback;
import com.js.nickcar.base.BasePresenter;
import com.js.nickcar.base.HttpResultFunc;
import com.js.nickcar.entity.remote.ApiResultBean;
import com.js.nickcar.entity.remote.LoginBean;
import com.zyzd.commontools.string.RegexUtil;

import rx.Observable;
import rx.Subscriber;

import static com.zyzd.commontools.string.RegexUtil.getString;

/**
 * Created by LZY on 2017/9/28.
 */
public class LoginPresenter extends BasePresenter<LoginView> {
    private long verCodeTime;

    /**
     * 获取验证码
     *
     * @param phone
     */
    public void getCode(String phone) {
        if (!RegexUtil.isMobile(phone)) {
            showToast(getString(R.string.please_input_valid_phone));
            return;
        }

        Observable<ApiResultBean> smsCode = HttpHelpter.getApiService().getSmsCode(phone);
        addSubscription(smsCode.map(new HttpResultFunc()), getSmsCodeSubsrciber());
    }

    private Subscriber getSmsCodeSubsrciber() {
        return new ApiCallback() {
            @Override
            public void onSuccess(Object model) {
                verCodeTime = System.currentTimeMillis();
                //获取验证码成功
                mView.getCodeSuccess();
            }

            @Override
            public void onFailure(String msg) {
                //获取验证码失败
                mView.showToast(msg);
            }

            @Override
            public void onFinish() {

            }
        };
    }


    public void login(String phone, String code) {
        if (!RegexUtil.isMobile(phone)) {
            showToast(getString(R.string.please_input_valid_phone));
            return;
        }

        if (!RegexUtil.isMobile(code)) {
            showToast(getString(R.string.please_input_valid_phone));
            return;
        }

        Observable<LoginBean> loginObservable = HttpHelpter.getApiService().login(phone, code)
                .map(new HttpResultFunc<LoginBean>());
        addSubscription(loginObservable, getLoginSubscriber());

    }

    private Subscriber<LoginBean> getLoginSubscriber() {
        return new ApiCallback<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                //登录成功
                if (loginBean == null) {
                    showToast("登录失败，获取信息异常");
                    return;
                }

                //保存用户信息
                UserHelper.updateUserId(loginBean.getAppraiser_id());
                UserHelper.updateToken(loginBean.getToken());
                UserHelper.updateUserInfo(loginBean);

                //通知界面登录成功
                mView.loginSuccess();
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }

            @Override
            public void onFinish() {

            }
        };
    }

    public long getVerCodeTime() {
        return verCodeTime;
    }

    public void setVerCodeTime(long verCodeTime) {
        this.verCodeTime = verCodeTime;
    }

    public int getSurplusTime() {
        return (int) ((verCodeTime + 30000 - System.currentTimeMillis()) / 1000);
    }
}
